<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="./js/data.js"></script>
    <script>
        console.log(data);
        console.log('------------------every-------------');
        // 如果有一个不满足条件就停止遍历，条件就是return后面的表达式
        data.every(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
            return elem.is_free == 1
        }, {
            name: 'text'
        })
        console.log('------------------some-------------');
        // 有一个满足条件就停止遍历，条件就是rerurn后面的表达式
        data.some(function(elem, index, array) {
            console.log(elem);
            console.log(index);
            console.log(array);
            console.log(this);
            return elem.is_free == 1
        }, {
            name: 'text'
        })
        Array.prototype.myEvery = function(fn) {
            var arr = this,
                len = arr.length,
                arg2 = arguments[1] || window,
                res = true;
            for (var i = 0; i < len; i++) {
                if (!fn.apply(arg2, [arr[i], i, arr])) {
                    res = false;
                    break;
                }
            }
            return res
        }
        console.log('------------------reduce-------------');
        // reduce 归纳函数 
        var initialValue = []
            // prev每次return后，后面遍历才会有值
            // prev是一个容器，满足要求就可以把数据放在prev中，然后return
            // Array.prototype.filter方法是一个根据布尔值筛选
        var nArr = data.reduce(function(prev, elem, index, arr) {
            console.log(prev);
            console.log(elem);
            console.log(index);
            console.log(arr);
            if (elem.is_free === '1') {
                prev.push(elem.course)
            }
            return prev
        }, initialValue)
        console.log(nArr);
        // reduce方法  倒序
        var nArr = data.reduceRight(function(prev, elem, index, arr) {
            console.log(prev);
            console.log(elem);
            console.log(index);
            console.log(arr);
            if (elem.is_free === '1') {
                prev.push(elem.course)
            }
            return prev
        }, initialValue)
        console.log(nArr);
        Array.prototype.myReduce = function(fn, initialValue) {
            var arr = this,
                len = arr.length,
                arg2 = arguments[2] || window,
                item;
            for (var i; i < len; i++) {
                initialValue = fn.apply(arg2, [initialValue, arr[i], i, arr])
            }
            return initialValue
        }
    </script>
</body>

</html>